﻿//------------------------------------------------------------------------------
// 创建标识: Copyright (C) 2008 Socansoft.com 版权所有
// 创建描述: SocanCode代码生成器自动创建于 2008-8-13 17:43:17
//
// 功能描述: 
//
// 修改标识: 
// 修改描述: 
//------------------------------------------------------------------------------

using System;
using System.Data;
using DALFactory;
using Model;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.Caching;

namespace BLL
{
    /// <summary>
    /// 业务逻辑类 FUNCTIONS
    /// </summary>
    public class FUNCTIONS : RemoveCaches
    {
        private readonly IDAL.IFUNCTIONS dal = DataAccess.CreateFUNCTIONS();
        public FUNCTIONS()
            : base("FUNCTIONS_")
        { }

        #region  成员方法
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public string Add(Model.FUNCTIONS model)
        {
            string data = dal.Add(model);
            if (Caches.EnableCache && data != null)
            {
                RemoveCountCache();
                RemoveMaxCache();
                RemoveListCache();
            }
            return data;
        }

        /// <summary>
        /// 增加多条数据
        /// </summary>
        public void Add(List<Model.FUNCTIONS> l)
        {
            foreach (Model.FUNCTIONS model in l)
                dal.Add(model);

            if (Caches.EnableCache)
            {
                RemoveCountCache();
                RemoveMaxCache();
                RemoveListCache();
            }
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Model.FUNCTIONS model)
        {
            bool b = dal.Update(model);
            if (Caches.EnableCache && b)
            {
                RemoveModelCache(model.FUNC_CODE);
                RemoveListCache();
                RemoveCountCache();
            }
            return b;
        }

        /// <summary>
        /// 更新多条数据
        /// </summary>
        public void Update(List<Model.FUNCTIONS> l)
        {
            foreach (Model.FUNCTIONS model in l)
            {
                bool b = dal.Update(model);
                if (Caches.EnableCache && b)
                    RemoveModelCache(model.FUNC_CODE);
            }
            if (Caches.EnableCache)
            {
                RemoveListCache();
                RemoveCountCache();
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(string FUNC_CODE)
        {
            bool b = dal.Delete(FUNC_CODE);
            if (Caches.EnableCache && b)
            {
                RemoveExistsCache(FUNC_CODE);
                RemoveModelCache(FUNC_CODE);
                RemoveListCache();
                RemoveMaxCache();
                RemoveCountCache();
            }
            return b;
        }

        /// <summary>
        /// 删除多条数据
        /// </summary>
        public void Delete(List<string> l)
        {
            foreach (string FUNC_CODE in l)
            {
                bool b = dal.Delete(FUNC_CODE);
                if (Caches.EnableCache && b)
                {
                    RemoveExistsCache(FUNC_CODE);
                    RemoveModelCache(FUNC_CODE);
                }
            }
            if (Caches.EnableCache)
            {
                RemoveListCache();
                RemoveMaxCache();
                RemoveCountCache();
            }
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string FUNC_CODE)
        {
            if (!Caches.EnableCache)
                return dal.Exists(FUNC_CODE);

            string key = basicKey + "Exists_" + FUNC_CODE.ToString();
            if (HttpRuntime.Cache[key] != null)
                return (bool)HttpRuntime.Cache[key];
            else
            {
                bool data = dal.Exists(FUNC_CODE);
                AggregateCacheDependency cd = CacheDependencyFactory.DependencyFacade.GetFUNCTIONSCacheDependency();
                Caches.TryAddCache(key, data, cd, Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(20), CacheItemPriority.Normal, null);
                return data;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.FUNCTIONS GetModel(string FUNC_CODE)
        {
            if (!Caches.EnableCache)
                return dal.GetModel(FUNC_CODE);

            string key = basicKey + "Model_" + FUNC_CODE.ToString();
            if (HttpRuntime.Cache[key] != null)
                return (Model.FUNCTIONS)HttpRuntime.Cache[key];
            else
            {
                Model.FUNCTIONS data = dal.GetModel(FUNC_CODE);
                AggregateCacheDependency cd = CacheDependencyFactory.DependencyFacade.GetFUNCTIONSCacheDependency();
                Caches.TryAddCache(key, data, cd, Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(20), System.Web.Caching.CacheItemPriority.Normal, null);
                return data;
            }
        }

        /// <summary>
        /// 得到数据总条数
        /// </summary>
        public int GetCount()
        {
            if (!Caches.EnableCache)
            {
                DataSet ds = dal.GetDataSet("count(*)", null, null);
                return Convert.ToInt32(ds.Tables[0].Rows[0][0]);
            }

            string key = basicKey + "Counts_All";
            if (HttpRuntime.Cache[key] != null)
                return (int)HttpRuntime.Cache[key];
            else
            {
                DataSet ds = dal.GetDataSet("count(*)", null, null);
                int data = Convert.ToInt32(ds.Tables[0].Rows[0][0]);
                AggregateCacheDependency cd = CacheDependencyFactory.DependencyFacade.GetFUNCTIONSCacheDependency();
                Caches.TryAddCache(key, data, cd, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.Normal, null);
                return data;
            }
        }

        /// <summary>
        /// 获得泛型数据列表,不建议直接使用此方法,请根据业务逻辑重写
        /// </summary>
        public List<Model.FUNCTIONS> GetList(string strWhat, string strWhere, string strOrderby)
        {
            if (!Caches.EnableCache)
                return dal.GetList(strWhat, strWhere, strOrderby) as List<Model.FUNCTIONS>;

            string key = basicKey + "List_" + strWhat + strWhere + strOrderby;
            if (HttpRuntime.Cache[key] != null)
                return (List<Model.FUNCTIONS>)HttpRuntime.Cache[key];
            else
            {
                List<Model.FUNCTIONS> data = dal.GetList(strWhat, strWhere, strOrderby) as List<Model.FUNCTIONS>;
                AggregateCacheDependency cd = CacheDependencyFactory.DependencyFacade.GetFUNCTIONSCacheDependency();
                Caches.TryAddCache(key, data, cd, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.Normal, null);
                return data;
            }
        }

        /// <summary>
        /// 分页获取泛型数据列表,不建议直接使用此方法,请根据业务逻辑重写
        /// </summary>
        public List<Model.FUNCTIONS> GetList(int pageSize, int pageIndex, string fldSort, bool sort, string strCondition, out int pageCount, out int count)
        {
            if (!Caches.EnableCache)
                return dal.GetList(pageSize, pageIndex, fldSort, sort, strCondition, out pageCount, out count) as List<Model.FUNCTIONS>;

            string key = basicKey + "List_" + fldSort + "_" + sort.ToString() + "_" + strCondition + "_" + pageSize.ToString() + "_" + pageIndex.ToString();
            if (HttpRuntime.Cache[key] != null)
            {
                Model.PageData data = (Model.PageData)HttpRuntime.Cache[key];
                pageCount = data.PageCount;
                count = data.Count;
                return (List<Model.FUNCTIONS>)data.Data;
            }
            else
            {
                List<Model.FUNCTIONS> lst = dal.GetList(pageSize, pageIndex, fldSort, sort, strCondition, out pageCount, out count) as List<Model.FUNCTIONS>;
                Model.PageData data = new Model.PageData(lst, count, pageCount);
                AggregateCacheDependency cd = CacheDependencyFactory.DependencyFacade.GetFUNCTIONSCacheDependency();
                if (pageIndex <= 3)
                    Caches.TryAddCache(key, data, cd, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.High, null);
                else
                    Caches.TryAddCache(key, data, cd, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.Normal, null);
                return lst;
            }
        }
        #endregion

        #region  扩展方法
        /// <summary>
        /// 获得全部泛型数据列表
        /// </summary>
        public List<Model.FUNCTIONS> GetAllList()
        {
            return GetList(null, null, null);
        }
        #endregion

        /// <summary>
        /// 根据功能编号 获取单据编号
        /// </summary>
        public string GetDM(string FUNC_CODE)
        {
            SQLServerDAL.FUNCTIONS f = new SQLServerDAL.FUNCTIONS();
            return f.GetDM(FUNC_CODE);
        }

        /// <summary>
        /// 根据功能编号 增加最大流水号
        /// </summary>
        public void ADDLSH(string FUNC_CODE)
        {
            SQLServerDAL.FUNCTIONS f = new SQLServerDAL.FUNCTIONS();
            f.ADDLSH(FUNC_CODE);
        }

        public DataSet GetAllFunctions()
        {
            return new SQLServerDAL.FUNCTIONS().GetAllFunctions();
        }
    }
}
